﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace jsGotoDefinition.VS2010.Helpers
{
    /// <summary>
    /// An object representing a list of FindResult objects 
    /// </summary>
    public class FindResultCollection : List<FindResult>
    {
        /// <summary>
        /// Empty constructor
        /// </summary>
        public FindResultCollection() {

        }

        /// <summary>
        /// Overloaded constructor accepting "Find Results" window text as input as well as the search phrase
        /// </summary>
        /// <param name="resultWindowText"></param>
        /// <param name="searchphrase"></param>
         public void AddFromResultWindowText(string resultwindowtext, string searchphrase) {
            AddFromResultWindowText(resultwindowtext, searchphrase, null, -1);
        }

         public void AddOrdered(FindResult findresult, string localfilepath, int searchtextline) {
             if (findresult.IsValid) {
                 //only add the result if there isn't already the same line number because of a diferent regex
                 bool found = false;
                 foreach (FindResult item in this) {
                     if (item.LineNumber == findresult.LineNumber && item.FileName == findresult.FileName) {
                         found = true;
                         break;
                     }
                 }
                 if (!found) {
                     //check if we are still in the current file, if so we want to order the results by the one closest to
                     if (localfilepath == findresult.FileName && this.Count > 0) {
                         int insertpos = 0;
                         foreach (FindResult item in this) {
                             if (Math.Abs(searchtextline - item.LineNumber) >= Math.Abs(searchtextline - findresult.LineNumber)) {
                                 break;
                             }
                             insertpos++;
                         }
                         if (insertpos == this.Count)
                             this.Add(findresult);
                         else
                             this.Insert(insertpos, findresult);
                     }
                     else
                         this.Add(findresult);
                 }
             }
         }

        /// <summary>
         /// Overloaded constructor accepting "Find Results" window text as input as well as the search phrase. Also accepts a localfile param that is the path to the local file where the search term is initially found
        /// </summary>
        /// <param name="resultwindowtext"></param>
        /// <param name="searchphrase"></param>
        /// <param name="localfilepath"></param>
        /// <param name="searchtextline"></param>
        public void AddFromResultWindowText(string resultwindowtext, string searchphrase, string localfilepath, int searchtextline) {
            //for each line in the text we insert a new Result item
            string[] lines = Regex.Split(resultwindowtext, "\r\n|\r|\n");
            //we skip the first line because it contains information about the search
            //we skip the last line because it contains info about the results
            for (int i = 1; i < lines.Length - 1; i++) {
                FindResult result = new FindResult(lines[i].Trim());
                result.SearchText = searchphrase;
                 
                if (result.IsValid) {
                    //only add the result if there isn't already the same line number because of a diferent regex
                    bool found = false;
                    foreach (FindResult item in this) {
                        if (item.LineNumber == result.LineNumber && item.FileName == result.FileName) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        //check if we are still in the current file, if so we want to order the results by the one closest to
                        if (localfilepath == result.FileName && this.Count > 0) {
                            int insertpos = 0;
                            foreach (FindResult item in this) {
                                if (Math.Abs(searchtextline - item.LineNumber) >= Math.Abs(searchtextline - result.LineNumber)) {
                                    break;
                                }
                                insertpos++;
                            }
                            if (insertpos == this.Count)
                                this.Add(result);
                            else
                                this.Insert(insertpos, result);
                        }
                        else
                            this.Add(result);
                    }

                }
                    
            }
        }
    }
}
